package com.petShop.controllers;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.petShop.dao.CartDao;
import com.petShop.dao.GoodDao;
import com.petShop.dao.UserDao;
import com.petShop.models.Cart;
import com.petShop.models.Good;
import com.petShop.models.Userinfo;
import com.petShop.util.MyTool;

@Controller
@Transactional
@RequestMapping("/buy")
public class CartController {
	@Autowired
	UserDao userDao;
	@Autowired
	CartDao cartDao;
	@Autowired
	GoodDao goodDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getCart(HttpSession session){
		ModelAndView mav = new ModelAndView("/buy/showCart");
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		if(userinfo!=null){	
			//System.out.println(userinfo.getUserId());
			List<Cart> carts = cartDao.selectCarts(userinfo.getUserId());
			int allSum = 0;
			for(Cart c:carts){
				allSum += c.getSum();
			}
			//总价
			session.setAttribute("allSum", allSum);
			//显示购物车
			mav.addObject("carts", carts);
			//获取购物车数量
			session.setAttribute("allCount", MyTool.getCartAllcount(carts));
			
		}
		return mav;
	}
	@RequestMapping(value="/addOne",method=RequestMethod.GET)
	public ModelAndView addOne(@RequestParam String goodId,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		Cart cart = new Cart();
		if(goodId!=null){
			cart.setGoodId(Integer.parseInt(goodId));
		}
	
		if(userinfo!=null){	
			cart.setUserId(userinfo.getUserId());
			
			if(cartDao.selectCart(cart)==null){   //若原购物车中没有该商品
				cartDao.insertCart(cart);
			}else{
				cartDao.addCart(cart);			//若有，添加
			}
			
			//获取购物车数量
			List<Cart> carts = cartDao.selectCarts(userinfo.getUserId());
			session.setAttribute("allCount", MyTool.getCartAllcount(carts));
			
		}	
		String returnUrl = (String) session.getAttribute("returnUrl");
		mav.setViewName("redirect:"+returnUrl);
		return mav;
	}
	@RequestMapping(value="/addSome",method=RequestMethod.GET)
	public ModelAndView addSome(@RequestParam String goodId,@RequestParam String qty_item,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		
		Cart cart = new Cart();
		if(goodId!=null&&qty_item!=null){
			cart.setGoodId(Integer.parseInt(goodId));
			cart.setSome(Integer.parseInt(qty_item));
		}

		if(userinfo!=null){	
			cart.setUserId(userinfo.getUserId());
			if(cartDao.selectCart(cart)==null){   //若原购物车中没有该商品
				cartDao.insertSomeToCart(cart);
			}else{
				cartDao.addSome(cart);			//若有，添加
			}
			
			//获取购物车数量
			List<Cart> carts = cartDao.selectCarts(userinfo.getUserId());
			session.setAttribute("allCount", MyTool.getCartAllcount(carts));
			
		}	
		mav.setViewName("redirect:/pet/"+goodId);
		return mav;
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(@RequestParam String goodId,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		
		Cart cart = new Cart();
		if(goodId!=null){
			cart.setGoodId(Integer.parseInt(goodId));
		}
	
		if(userinfo!=null){	
			
			cart.setUserId(userinfo.getUserId());
			
			if(cartDao.selectCart(cart)==null){   //若原购物车中没有该商品
				cartDao.insertCart(cart);
			}else{
				cartDao.addCart(cart);			//若有，添加
			}
			
			//获取购物车数量
			List<Cart> carts = cartDao.selectCarts(userinfo.getUserId());		
			session.setAttribute("allCount", MyTool.getCartAllcount(carts));
			
		}	
		mav.setViewName("redirect:/buy");
		return mav;
	}
	@RequestMapping(value="/remove",method=RequestMethod.GET)
	public ModelAndView remove(@RequestParam String goodId,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		
		Cart cart = new Cart();
		if(goodId!=null){
			cart.setGoodId(Integer.parseInt(goodId));
		}
	
		if(userinfo!=null){	
			cart.setUserId(userinfo.getUserId());
			
			Cart c = cartDao.selectCart(cart);
			if(c.getCount()==1){
				cartDao.clear(cart);
			}else{
				cartDao.remove(cart);
			}		
		}	
		mav.setViewName("redirect:/buy");
		return mav;
	}
	@RequestMapping(value="/clear",method=RequestMethod.GET)
	public ModelAndView clear(@RequestParam String goodId,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		
		Cart cart = new Cart();
		if(goodId!=null){
			cart.setGoodId(Integer.parseInt(goodId));
		}
	
		if(userinfo!=null){	
			cart.setUserId(userinfo.getUserId());
			
			cartDao.clear(cart);
		}	
		mav.setViewName("redirect:/buy");
		return mav;
	}
	@RequestMapping(value="/clearAll",method=RequestMethod.GET)
	public ModelAndView clear(HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
	
		if(userinfo!=null){	
			cartDao.clearAll(userinfo.getUserId());   //清空购物车
		}	
		
		mav.setViewName("redirect:/buy");
		return mav;
	}
	
	@RequestMapping(value="/buyNow",method=RequestMethod.GET)
	public ModelAndView buyNow(@RequestParam String goodId,@RequestParam String qty_item,HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
	
		if(userinfo!=null&&goodId!=null&&qty_item!=null){	
			Good good = new Good();
			good.setGoodId(Integer.parseInt(goodId));
			good.setSales(Integer.parseInt(qty_item));
			goodDao.addSales(good);
		}	
		
		mav.setViewName("/buy/buySuccess");
		return mav;
	}
	@RequestMapping(value="/buySuccess",method=RequestMethod.GET)
	public ModelAndView buySuccess(HttpSession session){
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		Userinfo userinfo = userDao.selectUserByName(username);
		
		if(userinfo!=null){	
			
			List<Cart> carts = cartDao.selectCarts(userinfo.getUserId());
			for(Cart cart:carts){
				Good good = new Good();
				good.setGoodId(cart.getGoodId());
				good.setSales(cart.getCount());
				goodDao.addSales(good);
			}
			cartDao.clearAll(userinfo.getUserId());   //清空购物车
			//设置显示购物车总数
			session.setAttribute("allCount", 0);
		}	
		
		mav.setViewName("/buy/buySuccess");
		return mav;
	}
}
